/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.omega_r.omegarecyclerview.expandable_example.core;

import com.omega_r.libs.omegarecyclerview.base.BaseBean;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;

public class ExpandableViewHolder extends BaseViewHolder {
    private Text listAdapterItemTxt;
    private DirectionalLayout directionalLayout;
    private DirectionalLayout itemTop;

    public ExpandableViewHolder(Component itemView, ListContainer listContainer) {
        super(itemView, listContainer);
        listAdapterItemTxt = (Text) findComponentById(ResourceTable.Id_list_adapter_item_txt);
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_dir_add);
        itemTop = (DirectionalLayout) findComponentById(ResourceTable.Id_item_top);
    }

    public void updateView(BaseBean baseBean, boolean isCloseOtherItem, boolean isAnimator, int position) {
        super.updateView(baseBean);
        SimpleData listAdapterBean = (SimpleData) baseBean;
        listAdapterItemTxt.setText(listAdapterBean.getmTitle());

        directionalLayout.removeAllComponents();
        for (int j = 0; j < listAdapterBean.getmQuotes().size(); j++) {
            Component componentChild = LayoutScatter.getInstance(itemView.getContext()).parse(ResourceTable.Layout_item_core_child_adapter,
                    null, false);
            Text childText = (Text) componentChild.findComponentById(ResourceTable.Id_list_adapter_item_txt);
            childText.setText(listAdapterBean.getmQuotes().get(j));
            directionalLayout.addComponent(componentChild);
        }
        setItemExtend(directionalLayout, itemTop, baseBean, isCloseOtherItem, isAnimator, position, null);
    }
}
